package com.handpay.nocardpay.baselibrary.qualificationinfo.widgets;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.Context;
import android.graphics.Color;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.handpay.nocardpay.baselibrary.R;
import com.handpay.nocardpay.baselibrary.qualificationinfo.adapters.SubBankAdapter;
import com.handpay.nocardpay.baselibrary.qualificationinfo.model.bean.SubBankBean;
import com.handpay.nocardpay.baselibrary.utils.Heart;

import java.util.List;

@SuppressLint("AppCompatCustomView")
public class HPSubBankEditView extends EditText {

    SubBankAdapter mBankAdapter = null;
    private AlertDialog mAlertDialog;
    private OnBankLisenter mListener;
    private View mContainer;
    private EditText mEt;
    private ListView mBankListView;

    public HPSubBankEditView(Context context) {
        super(context);
        init();
    }

    public HPSubBankEditView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public HPSubBankEditView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    private void popDialog() {
        AlertDialog.Builder ab = new AlertDialog.Builder(this.getContext());
        TextView textView = new TextView(this.getContext());
        textView.setText(R.string.public_opening_bank_hint);
        textView.setBackgroundResource(R.color.colorBlue);
        textView.setTextColor(Color.WHITE);
        textView.setTextSize(15f);
        textView.setGravity(Gravity.CENTER_HORIZONTAL);
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        textView.setPadding(0, 20, 0, 20);
        textView.setLayoutParams(layoutParams);
        ab.setCustomTitle(textView);
        ab.setView(createContent());
        mAlertDialog = ab.create();
        mAlertDialog.show();
    }

    public void onStop() {
        closeDialog();
    }

    private void closeDialog() {
        try {
            if (mAlertDialog != null) {
                mAlertDialog.dismiss();
                mAlertDialog = null;
            }
        } catch (Exception e) {
            e.printStackTrace();
            mAlertDialog = null;
        }
    }

    public void init() {
        this.setFocusable(false);
        mBankAdapter = new SubBankAdapter(this.getContext());
        this.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                popDialog();
            }
        });
    }

    /**
     * 创建dialog的内容。这里每次都是重新创建对象。能否复用？
     *
     * @return
     */
    private View createContent() {
        mContainer = inflate(getContext(), R.layout.pop_subbank_dialog, null);
        mEt = (EditText) mContainer.findViewById(R.id.et_subbank);
        mEt.addTextChangedListener(mTextWatcher);
        mBankListView = (ListView) mContainer.findViewById(R.id.sub_bank_list);
        mBankListView.getLayoutParams().height = Heart.displayHeightPixels / 2;
        mBankListView.setDivider(this.getResources().getDrawable(R.color.grey_color));
        mBankListView.setDividerHeight(1);
        mBankListView.setAdapter(mBankAdapter);
        mBankListView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                mAlertDialog.dismiss();
                if (null != mListener) {
                    closeDialog();
                    mListener.onBankSelected((SubBankBean.ListBean) arg0.getAdapter().getItem(arg2));
                }
            }
        });
        return mContainer;
    }

    private TextWatcher mTextWatcher;

    @Override
    public void addTextChangedListener(TextWatcher watcher) {
        mTextWatcher = watcher;
    }

    public interface OnBankLisenter {
        void onBankSelected(SubBankBean.ListBean listBean);
    }

    public void setLisenter(OnBankLisenter listener) {
        mListener = listener;
    }

    public synchronized void refreshBanks(List<SubBankBean.ListBean> banks) {
        if(mBankAdapter!=null){
            mBankAdapter.setmLists(banks);
        }
    }

    public void clearList(){
        mBankAdapter.clear();
    }
}
